package com.thinkfit.jy.utils;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.common.lib.manager.DataManager;
import com.common.lib.utils.BaseUtils;
import com.thinkfit.jy.R;

/**
 * 显示速度和公里数
 */
public class ShowSpeedViewUtils implements View.OnClickListener{
    private static ShowSpeedViewUtils showSpeedViewUtils;
    WindowManager windowManager;
    View inflate;
    boolean isFist = true;
    boolean isClick = true;
    Context mContext;

    TextView tvHeart,tvSpeed,tvDistance,tvTime,tvKcal;
    ImageView ivUserHead;
    ConstraintLayout clItem,clSport;
    LinearLayout llSpeed,llTime,llDistance,llKcal;
    View v_1;
    int showType = 1;

    public static ShowSpeedViewUtils getInstance(){
        if(showSpeedViewUtils == null){
            showSpeedViewUtils = new ShowSpeedViewUtils();
        }
        return showSpeedViewUtils;
    }

    public void initView(Context context){
        mContext = context;
        if(windowManager == null){
            windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        }

        int language = DataManager.Companion.getInstance().getLanguage();
        System.out.println("aaaaaaaaaaaaaaaaaaaaaaaaa   language : "+language);
        BaseUtils.StaticParams.changeAppLanguage(context, language);

        if(inflate == null){
            inflate = LayoutInflater.from(context).inflate(R.layout.item_show_speed, null);
//            v_1.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    if(!isClick){
//                        return;
//                    }
//                    lessenAnimation();
////                    goneLayout();
////                    SportsStatusBarViewUtils.getInstance().goneLayout();
////                    BackButtonViewUtils.getInstance().initView(mContext);
//                }
//            });

        }

        clItem = inflate.findViewById(R.id.cl_item);
        clSport = inflate.findViewById(R.id.cl_sport);
        tvHeart = inflate.findViewById(R.id.tv_heart);
        tvSpeed = inflate.findViewById(R.id.tv_speed);
        tvDistance = inflate.findViewById(R.id.tv_distance);
        tvTime = inflate.findViewById(R.id.tv_time);
        tvKcal = inflate.findViewById(R.id.tv_kcal);
        ivUserHead = inflate.findViewById(R.id.iv_user_head);
        v_1 = inflate.findViewById(R.id.v_1);
        llSpeed = inflate.findViewById(R.id.ll_speed);
        llTime = inflate.findViewById(R.id.ll_time);
        llDistance = inflate.findViewById(R.id.ll_distance);
        llKcal = inflate.findViewById(R.id.ll_kcal);

//            UserBean myInfo = DataManager.Companion.getInstance().getMyInfo();
//            BaseUtils.StaticParams.loadImage(context, R.drawable.app_not_login, myInfo.getHeadimg(),ivUserHead);

        llSpeed.setOnClickListener(this);
        llTime.setOnClickListener(this);
        llDistance.setOnClickListener(this);
        llKcal.setOnClickListener(this);



        setDeviceImg();

//        ImageView ivBack = inflate.findViewById(R.id.iv_back);
//        ivBack.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Toast.makeText(context,"点击返回健",Toast.LENGTH_SHORT).show();
//
//                Intent intent = new Intent(context, MainActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                context.startActivity(intent);
//
//                windowManager.removeView(inflate);
//                isFist = true;
//            }
//        });


        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT
        );

        layoutParams.gravity = Gravity.TOP| Gravity. CENTER;
        layoutParams.y = dp2px(57);

        if(isFist){
            isFist = false;
            windowManager.addView(inflate,layoutParams);
        }else {
            inflate.setVisibility(View.VISIBLE);
            recoverAnimation();
        }

    }

    public void setDeviceImg(){
        switch (MotionDataUtils.mCurrentDeviceType){
            case DeviceTypeUtils.TREADMILL:
                ivUserHead.setImageResource(R.drawable.img_main_treadmill);
                break;
            case DeviceTypeUtils.ELLIPTICAL_MACHINE:
                ivUserHead.setImageResource(R.drawable.img_main_elliptical_machine);
                break;
            case DeviceTypeUtils.SPINNING:
                ivUserHead.setImageResource(R.drawable.img_main_spinning);
                break;
            case DeviceTypeUtils.ROWING_MACHINE:
                ivUserHead.setImageResource(R.drawable.img_main_rowing_machine);
                break;

        }
    }

    public void setInfo(String heart,String speed,String distance,String time,String kcal){
        if (inflate != null){
            tvHeart.setText(heart);
            tvDistance.setText(distance);
            tvTime.setText(time);
            tvKcal.setText(kcal);
            tvSpeed.setText(speed);
        }

    }

//    public void setInfo(SerialPortManager.CurrentInfo info){
//        if (inflate != null){
//
//            int hour = info.time / 3600;
//            int minute = (info.time / 60) % 60;
//            int second = info.time % 60;
//            String time ;
//            if (hour > 0) {
//                time = BaseUtils.StaticParams.getNewText(hour) + ":"
//                        + BaseUtils.StaticParams.getNewText(minute) + ":"
//                        + BaseUtils.StaticParams.getNewText(second);
//            } else {
//                time =  BaseUtils.StaticParams.getNewText(minute) + ":"
//                        + BaseUtils.StaticParams.getNewText(second);
//            }
//
//            tvHeart.setText(info.heart+"");
//            tvSpeed.setText(Utils.getNewFloat2(info.speed*0.01f));
//            tvDistance.setText(Utils.getNewFloat2(info.distance / 1000f));
//            tvTime.setText(time);
//            tvKcal.setText(Utils.getNewFloat(setKacl(info.kcal)));
//        }
//
//    }

    public void isOnClick(boolean isClick){
        this.isClick = isClick;
    }


    public void initInfo(){
        if(inflate != null){
            tvHeart.setText("-");
            tvDistance.setText("0.0");
            tvTime.setText("00:00");
            tvKcal.setText("0.0");
            tvSpeed.setText("0.0");
        }
    }

    AnimatorSet cassetteAnimatorSet;
    public void lessenAnimation(){

        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(clSport, "translationX", 0f, - dp2px(785));
        objectAnimator.setDuration(1000);
        ObjectAnimator animResizeX = ObjectAnimator.ofFloat(clSport, "scaleX", 1f, 0.3f);
        animResizeX.setDuration(500);
        ObjectAnimator animResizeY = ObjectAnimator.ofFloat(clSport, "scaleY", 1f, 0.3f);
        animResizeY.setDuration(500);
        cassetteAnimatorSet = new AnimatorSet();
        cassetteAnimatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
//                String msg = "恭喜你获得"+golds1+"个安全学分";
//                EventBusUtil.postMessage(msg,EventBusTags.XHL_PRESENTATION_TTS);

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                transparencyAnimation();

//                delayGetGold();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        cassetteAnimatorSet.playTogether(objectAnimator, animResizeX, animResizeY);//.after(rotationXAnimator)
        cassetteAnimatorSet.start();




//        TranslateAnimation animation = new TranslateAnimation(0, - dp2px(785), 0, 0);
//        animation.setDuration(1000);
//        animation.setFillAfter(true);
        // 创建缩放的动画对象
//        ScaleAnimation sa = new ScaleAnimation(1.0f, 0.10f, 1.0f, 1.0f, ScaleAnimation.RELATIVE_TO_SELF, 0f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
//        // 设置动画播放的时间
//        sa.setDuration(500);
//        sa.setFillAfter(true);
//        animation.setAnimationListener(new Animation.AnimationListener() {
//            @Override
//            public void onAnimationStart(Animation animation) {
//
//            }
//
//            @Override
//            public void onAnimationEnd(Animation animation) {
//                transparencyAnimation();

//                SportsStatusBarViewUtils.getInstance().goneLayout();
//                BackButtonViewUtils.getInstance().initView(mContext);
//                clItem.clearAnimation();
//            }
//
//            @Override
//            public void onAnimationRepeat(Animation animation) {
//
//            }
//        });
        // 开始播放动画
//        clSport.startAnimation(animation);
    }

    public void recoverAnimation(){
        System.out.println("ddddddddddddddddddddddd xxxxxxxx"+(inflate != null));
        if(inflate != null){
            System.out.println("ddddddddddddddddddddddd xxxxxxxx222");
            inflate.setVisibility(View.VISIBLE);
        }
        // 创建缩放的动画对象
//        ScaleAnimation sa = new ScaleAnimation(0f, 1.0f, 0f, 1.0f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
//        // 设置动画播放的时间
//        sa.setDuration(500);
//        System.out.println("ddddddddddddddddddddddd xxxxxxxx 1111");
//        sa.setAnimationListener(new Animation.AnimationListener() {
//            @Override
//            public void onAnimationStart(Animation animation) {
//                System.out.println("ddddddddddddddddddddddd xxxxxxxx444");
//                BackButtonViewUtils.getInstance().goneButton();
//            }
//
//            @Override
//            public void onAnimationEnd(Animation animation) {
//                SportsStatusBarViewUtils.getInstance().initView(mContext);
//                ApkListViewUtils.getInstance().goneButton();
//                clSport.clearAnimation();
//            }
//
//            @Override
//            public void onAnimationRepeat(Animation animation) {
//
//            }
//        });
//        System.out.println("ddddddddddddddddddddddd xxxxxxxx333");
//        // 开始播放动画
//        clSport.startAnimation(sa);

        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(clSport, "translationX", - dp2px(785), 0f);
        objectAnimator.setDuration(500);
        ObjectAnimator animResizeX = ObjectAnimator.ofFloat(clSport, "scaleX", 0.3f, 1f);
        animResizeX.setDuration(1000);
        ObjectAnimator animResizeY = ObjectAnimator.ofFloat(clSport, "scaleY", 0.3f, 1f);
        animResizeY.setDuration(1000);
        cassetteAnimatorSet = new AnimatorSet();
        cassetteAnimatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

                BackButtonViewUtils.getInstance().goneButton();
                SportsStatusBarViewUtils.getInstance().transparencyAnimation(mContext,true);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                ApkListViewUtils.getInstance().goneButton();
                clSport.clearAnimation();
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        cassetteAnimatorSet.playTogether(objectAnimator, animResizeX, animResizeY);//.after(rotationXAnimator)
        cassetteAnimatorSet.start();
    }

    public void transparencyAnimation(){
        AlphaAnimation alphaAnimation = new AlphaAnimation(1f, 0f);
        alphaAnimation.setDuration(500);
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                SportsStatusBarViewUtils.getInstance().transparencyAnimation(mContext,false);
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                goneLayout();
                BackButtonViewUtils.getInstance().showAnimation(mContext,showType);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        clSport.startAnimation(alphaAnimation);
    }



    public void goneLayout(){
        if(inflate != null){
            inflate.setVisibility(View.GONE);
        }
    }

    /**
     * dp2px
     */
    protected int dp2px(float dpValue) {
        float density = mContext.getResources().getDisplayMetrics().density;
        return (int) (dpValue * density + 0.5f);
    }

    @Override
    public void onClick(View v) {
        if(!isClick){
            return;
        }
        switch (v.getId()){
            case R.id.ll_speed:
                showType = 1;
                lessenAnimation();
                break;
            case R.id.ll_time:
                showType = 2;
                lessenAnimation();
                break;
            case R.id.ll_distance:
                showType = 3;
                lessenAnimation();
                break;
            case R.id.ll_kcal:
                showType = 4;
                lessenAnimation();
                break;

        }
    }

    public void invaliView(){
        if(inflate != null){
            windowManager.removeView(inflate);
            isFist = true;
            windowManager = null;
            inflate = null;
        }
    }
}
